#!/bin/bash
./clean.sh

# 项目根目录
PROJECT_ROOT="$(dirname "$(readlink -f "$0")")"

# .pro 文件路径
PRO_FILE_NAME="application.pro"
PRO_FILE="$PROJECT_ROOT/$PRO_FILE_NAME"

# 检查 .pro 文件是否存在
if [ ! -f "$PRO_FILE" ]; then
    echo "Error: $PRO_FILE does not exist."
    exit 1
fi

TMP_PRO=".lib.pro"
cp $PRO_FILE $TMP_PRO



# 使用 find 命令收集所有的收集所有的子目录路径、 .h、.cpp 、 .ui 、.qrc文件
SUBDIRS=$(find -L ./src -type d)
HEADERS_FILES=$(find ./ -name '*.h')
SOURCES_FILES=$(find ./ -name '*.cpp')
FORMS_FILES=$(find ./ -name '*.ui')
QRC_FILES=$(find ./ -name '*.qrc')

# 将所有的目录进行拼接至列表中
SUBDIRS_LIST=""
for subdir in $SUBDIRS; do
    SUBDIRS_LIST+=" $subdir"
done

HEADERS_LIST=""
for file in $HEADERS_FILES; do
    HEADERS_LIST+=" $file"
done

SOURCES_LIST=""
for file in $SOURCES_FILES; do
    SOURCES_LIST+=" $file"
done

FORMS_LIST=""
for file in $FORMS_FILES; do
    FORMS_LIST+=" $file"
done

QRC_LIST=""
for file in $QRC_FILES; do
    QRC_LIST+=" $file"
done

# 将列表替换掉.pro中对应的FLAG
sed -i "s#INCFLAGS#$SUBDIRS_LIST#g" $TMP_PRO
sed -i "s#HEADERSFLAGS#$HEADERS_LIST#g" $TMP_PRO
sed -i "s#SOURCESFLAGS#$SOURCES_LIST#g" $TMP_PRO
sed -i "s#UIFLAGS#$FORMS_LIST#g" $TMP_PRO
sed -i "s#RESFLAGS#$QRC_LIST#g" $TMP_PRO

# 输出信息
echo "Subdirectories, headers, sources, and forms have been added to $TMP_PRO"

qmake $TMP_PRO
make -j8